package com.sgb.goods.widget.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewStub;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.goods.R;
import com.sgb.goods.utils.base.adapter.BaseCommonAdapter;
import com.sgb.goods.widget.MaxHeightRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

/**
 * 订单动态弹窗
 *
 * @author cyj
 */
public abstract class SimpleTile1Pop<T> extends BasePopWindow<T> {

    protected TextView mTitle;
    /**
     * 空布局
     */
    protected ViewStub viewStub;

    public SimpleTile1Pop(Context context) {
        super(context, DIR_DOWN_UP);
    }

    /**
     * 获取adapter实例
     *
     * @return BaseRecycleAdapter
     */
    protected abstract BaseCommonAdapter<T> getAdapter();

    @Override
    protected int popLayout() {
        return R.layout.goods_pop_simple_title_1;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mTitle = holder.getPop().findViewById(R.id.title);
        holder.getPop().findViewById(R.id.close).setOnClickListener(v -> dismiss());
        MaxHeightRecyclerView maxHeightRecyclerView = holder.getPop().findViewById(R.id.recyclerView);
        maxHeightRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        maxHeightRecyclerView.setAdapter(getAdapter());
        viewStub = holder.getPop().findViewById(R.id.goods_view_no_data);
    }

    @Override
    public void showDownPopwindow(View view, boolean alpha) {
        if (!this.isShowing()) {
            showAtLocation(view, Gravity.BOTTOM, 0, 0);
            if (bgView != null) {
                if (alpha) {
                    setAlpha();
                }
            }
        } else {
            dissMiss();
        }
    }

    private void setAlpha() {
        Window window = ((Activity) mContext).getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 0.4f;
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);
        setTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
    }

}
